package com.relytosoft.mizar.media.analyze.services.service.behavior.impl;

import com.relytosoft.mizar.media.analyze.services.service.behavior.AyBehaviorControlService;
import com.relytosoft.mizar.media.analyze.services.service.behavior.AyBehaviorDataService;
import com.relytosoft.mizar.media.common.entity.pojo.request.btdl.common.BtdlBehaviorDataReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author wss
 * @version 1.0
 * @date 2022/6/14 9:28
 */
@Service
public class AyBehaviorDataServiceImpl implements AyBehaviorDataService {

    static ThreadPoolExecutor threadPoolExecutor;

    static {
        threadPoolExecutor = new ThreadPoolExecutor(
                (int) (Runtime.getRuntime().availableProcessors() * 1.5),
                Runtime.getRuntime().availableProcessors() * 3,
                60L,
                TimeUnit.SECONDS,
                //选用无界的阻塞队列 则设置的mumPoolSize参数没有意义
                new LinkedBlockingDeque<Runnable>(10000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );
    }

    protected Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AyBehaviorControlService ayBehaviorControlService;

    @Override
    public void receiveRawData(BtdlBehaviorDataReq btdlBehaviorDataReq) {
        threadPoolExecutor.execute(() -> ayBehaviorControlService.rawBehaviorVerify(btdlBehaviorDataReq));
    }
}
